﻿#include "CAlgorithmBase.h"

CAlgorithmBase::CAlgorithmBase()
{
	m_nLastConRollerNum = 0;
	m_nDirection = 0;
}

CAlgorithmBase::CAlgorithmBase(QString sSceneId)
{
}

void CAlgorithmBase::init(CDevBase * pDev, CRBaseData * pRBasicData)
{
	m_pRBasicData = pRBasicData;
	m_pDev = pDev;
}

CAlgorithmBase::~CAlgorithmBase()
{
}

bool CAlgorithmBase::actionAlgorthm(SCPolItemCom * pPolItemCom,QString sTaskName)
{
	return false;
}

bool CAlgorithmBase::stopAlgorthm(SCPolItemCom * pPolItemCom)
{
	return false;
}
void CAlgorithmBase::closeCotDet()
{
}
bool CAlgorithmBase::isCollectRoller(int nVal, int nInterval)
{
	/**
	* @ Author: Gaojia
	* @ Description: 如果没设置间隔 默认2个托辊间隔
	*/
	if (nInterval <= 0)
	{
		nInterval = 2;
	}
	/**
	* @ Author: Gaojia
	* @ Description: 起始点或者中间点直接采集
	*/
	if (m_bConllectBegin)
	{
		m_nDirection = 0;
		m_nPositive = 0;
		m_nReverse = 0;
		m_bConllectBegin = false;
		m_nLastConRollerNum = nVal;
		return true;
	}
	/**
	* @ Author: Gaojia
	* @ Description: 间隔dInterval采集一次
	*/
	int nDiff = nVal - m_nLastConRollerNum;

	if ((nDiff == 0) || ((nDiff % nInterval) != 0))
	{
		return false;
	}
	//无方向
	if (m_nDirection == 0)
	{
		if (nVal > m_nLastConRollerNum)
		{
			m_nPositive++;
			if (m_nPositive > 2)
			{
				m_nDirection = 1;
				m_nReverse = 0;
			}
		}
		else
		{
			m_nReverse++;
			if (m_nReverse > 2)
			{
				m_nDirection = 2;
				m_nPositive = 0;
			}
		}
		m_nLastConRollerNum = nVal;
		return true;
	}
	//正向
	else if (m_nDirection == 1)
	{
		if (nDiff > 0)
		{
			m_nLastConRollerNum = nVal;
			return true;
		}
	}
	//反向
	else
	{
		if (nDiff < 0)
		{
			m_nLastConRollerNum = nVal;
			return true;
		}
	}
	return false;
}


bool CAlgorithmBase::isCollectMeter(double dVal, double dInterval)
{
	/**
	* @ Author: Gaojia
	* @ Description: 和最近的一次报警小于一米 并且和最近的一次采集小于dInterval 不自动采集
	*/
	if (dInterval <= 0)
	{
		dInterval = 3;
	}

	/**
	* @ Author: Gaojia
	* @ Description: 起始点或者中间点直接采集
	*/
	if (m_bConllectBegin)
	{
		m_bConllectBegin = false;
		m_dLastConLocation = dVal;
		return true;
	}

	/**
	* @ Author: Gaojia
	* @ Description: 间隔dInterval采集一次
	*/
	double dConLocationVal = abs(dVal - m_dLastConLocation);
	if (dConLocationVal > dInterval)
	{
		if (dVal > m_dLastConLocation)
		{
			while (abs(dVal - m_dLastConLocation) > dInterval)
			{
				m_dLastConLocation = m_dLastConLocation + dInterval;
			}
		}
		else
		{
			while (abs(dVal - m_dLastConLocation) > dInterval)
			{
				m_dLastConLocation = m_dLastConLocation - dInterval;
			}
		}
		return true;
	}

	/**
	* @ Author: Gaojia
	* @ Description: 和上次报警检测超过1米 采集数据
	*/
	double dLocationVal = abs(dVal - m_dLastLocation);
	if (dLocationVal > 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}